"""
文件相关的类定义
"""
import json

from  data_define import Record

class FileReader:

    def read_data(self)->list[Record]:
        # 读取文件数据，将每一条数据转换为Record对象，封装到list内返回即可
        pass

class TextFileReader(FileReader):

    def __init__(self, filename):
        self.filename = filename

    def read_data(self) -> list[Record]:
        f = open(self.filename,'r',encoding="utf-8")

        record_list: list[Record] = []
        for line in f.readlines():
            line = line.strip()
            print(line)
            data_list = line.split(",")
            record = Record(data_list[0], data_list[1],int(data_list[2]), data_list[3])
            record_list.append(record)

        f.close()
        return record_list

class JsonFileReader(FileReader):

    def __init__(self, filename):
        self.filename = filename

    def read_data(self) -> list[Record]:
        f = open(self.filename,'r',encoding="utf-8")

        record_list: list[Record] = []
        for line in f.readlines():
            data_dict = json.loads(line)
            print(data_dict)
            record = Record(data_dict["date"],data_dict["order_id"],data_dict["money"],data_dict["province"])
            record_list.append(record)

        f.close()
        return record_list


if __name__ == '__main__':
    text_file_reader = TextFileReader("2025销售数据")
    list1 = text_file_reader.read_data()
    # json_file_reader = JsonFileReader("2025销售数据Json")
    # list2 = json_file_reader.read_data()